﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLTVNEW
{
    public partial class frmSearchDocGia : DevComponents.DotNetBar.Office2007Form
    {
        public frmSearchDocGia()
        {
            InitializeComponent();
        }

        QLTVDataContext db = new QLTVDataContext();
        void GetAutoComplete(int i)
        {
            AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
            auto.Clear();
            if (i ==0 )
            {
                var tbHT = from tbDG in db.DOCGIAs select tbDG.HoTen;
                if (tbHT!=null)
                {
                    foreach (var item in tbHT)
                    {
                        auto.Add(item.Trim());
                    }
                    txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
                    txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    txtSearch.AutoCompleteCustomSource = auto;
                }
            }
            if (i==1)
            {
                var tbLoai = from tb in db.LOAIDOCGIAs select tb.TenLoaiDocGia;
                if (tbLoai!=null)
                {
                    foreach (var item in tbLoai)
                    {
                        auto.Add(item.Trim());
                    }
                    txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
                    txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    txtSearch.AutoCompleteCustomSource = auto;
                }
            }
            if (i==2)
            {
                var tbTT = from tb in db.DOCGIAs select tb.TinhTrangThe;
                if (tbTT!=null)
                {
                    foreach (var item in tbTT)
                    {
                        auto.Add(item.Trim());
                    }
                    txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
                    txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    txtSearch.AutoCompleteCustomSource = auto;
                }
            }
        }

        private void frmSearchDocGia_Load(object sender, EventArgs e)
        {

        }

     


        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            if (rdbTen.Checked == false && rdbTTrang.Checked == false && rdbLoai.Checked == false)
            {
               
                MessageBox.Show("Bạn Chưa Chọn Kiểu Tìm Kiếm", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                
            }
            else
            {
                if (rdbTen.Checked==true)
                {
                    var query = (from tbDG in db.DOCGIAs
                                 join tbLoai in db.LOAIDOCGIAs on tbDG.MaLoaiDocGia equals tbLoai.MaLoaiDocGia
                                 where tbDG.HoTen.Contains(txtSearch.Text)
                                 select new
                                 {
                                     tbDG.MaDocGia,
                                     tbDG.HoTen,
                                     tbDG.NgaySinh,
                                     tbDG.DiaChi,
                                     tbDG.Email,
                                     tbDG.NgayLapThe,
                                     tbDG.NgayHetHan,
                                     tbLoai.TenLoaiDocGia,
                                     tbDG.SoSachDangMuon,
                                     tbDG.TinhTrangThe
                                 });
                    dataGridViewX1.DataSource = query;
                }
                if (rdbLoai.Checked==true)
                {
                    var query = (from tbDG in db.DOCGIAs
                                 join tbLoai in db.LOAIDOCGIAs on tbDG.MaLoaiDocGia equals tbLoai.MaLoaiDocGia
                                 where tbLoai.TenLoaiDocGia.Contains(txtSearch.Text)
                                 select new
                                 {
                                     tbDG.MaDocGia,
                                     tbDG.HoTen,
                                     tbDG.NgaySinh,
                                     tbDG.DiaChi,
                                     tbDG.Email,
                                     tbDG.NgayLapThe,
                                     tbDG.NgayHetHan,
                                     tbLoai.TenLoaiDocGia,
                                     tbDG.SoSachDangMuon,
                                     tbDG.TinhTrangThe
                                 });
                    dataGridViewX1.DataSource = query;
                }
                if (rdbTTrang.Checked==true)
                {
                    var query = (from tbDG in db.DOCGIAs
                                 join tbLoai in db.LOAIDOCGIAs on tbDG.MaLoaiDocGia equals tbLoai.MaLoaiDocGia
                                 where tbDG.TinhTrangThe.Contains(txtSearch.Text)
                                 select new
                                 {
                                     tbDG.MaDocGia,
                                     tbDG.HoTen,
                                     tbDG.NgaySinh,
                                     tbDG.DiaChi,
                                     tbDG.Email,
                                     tbDG.NgayLapThe,
                                     tbDG.NgayHetHan,
                                     tbLoai.TenLoaiDocGia,
                                     tbDG.SoSachDangMuon,
                                     tbDG.TinhTrangThe
                                 });
                    dataGridViewX1.DataSource = query;
                }
            }
        }

        private void rdbTen_CheckedChanged(object sender, EventArgs e)
        {
            GetAutoComplete(0);
        }

        private void rdbLoai_CheckedChanged(object sender, EventArgs e)
        {
            GetAutoComplete(1);
        }

        private void rdbTTrang_CheckedChanged(object sender, EventArgs e)
        {
            GetAutoComplete(2);
        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
        }
    }
}
